<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/12/3
 * Time: 16:22
 */

namespace app\common\mysic\WxError;


use app\common\mysic\WxRefundErrorResponseHandle;

class RefundFullyBalance extends WxRefundErrorResponseHandle implements WxResponseErrorHandlerInterface
{
    public function handle()
    {
        try {
            $this->log->info('开始', '微信记录的分红余额已为0，分红出局');
            $this->bonus = $this->receivesEntity->balance;
            $ret = $this->bonusReceiveOp
                ->setUserModel($this->userModel)
                ->setReceiveEntity($this->receivesEntity)
                ->setBonus($this->bonus)
                ->update(1, 1);
            $this->log->info(bool2str($ret), '微信记录的分红余额已为0，分红出局结果');
        } catch (\Throwable $e) {
            $this->log->error(exceptionLogFormat($e), '更新用户分红列表信息主表时抛出异常');
            return false;
        }
        if($this->bonus > 0) {
            $this->log->info('开始', '更新分红记录副表');
            try {
                $ret = $this->bonusReceiveUserOp
                    ->setBonusSetting($this->bonusSettingEntity)
                    ->setRefundType(1)
                    ->setOtherOrder($this->paidUserOrderEntity)
                    ->setRefundData('')
                    ->setBonus($this->bonus)
                    ->setReceiveEntity($this->receivesEntity)
                    ->setModel($this->bonusReceiveUserModel)
                    ->append();
                $this->log->info(bool2str($ret), '更新分红记录副表结果');
            } catch (\Throwable $e) {
                $this->log->error(exceptionLogFormat($e), '更新分红记录副表时抛出异常');
                $this->log->info('', '业务结束');
                return false;
            }
        }
        return true;
    }
}